package com.ddzh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddzh.mapper.OrderSettingMapper;
import com.ddzh.pojo.OrderSetting;
import com.ddzh.service.OrderSettingService;
import com.ddzh.utils.DateUtil;
import org.apache.dubbo.config.annotation.Service;

import java.util.*;

@Service
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    /**
     * 解析结果保存到数据库中
     * @param data
     * @return
     */
    @Override
    public boolean importData(List<String[]> data) {
        //对集合判nullif
        if (data !=null){
            List<OrderSetting> dataList = new ArrayList<>();
            //遍历集合
            for (String[] row : data) {
                //取出每一列数据
                String orderDate = row[0];
                String orderNumber = row[1];
                //判断记录行是否已经存在
                OrderSetting orderSetting = findByDate(orderDate);
                if (orderSetting ==null){//不存在=》创建一条新记录
                    orderSetting = new OrderSetting();
                }
                //添加 / 修改数量
                orderSetting.setOrderNumber(Integer.valueOf(orderNumber));
                orderSetting.setOrderDate(DateUtil.parseDate(orderDate));
                dataList.add(orderSetting);
            }
            //批量 插入 / 修改数据库
            this.saveOrUpdateBatch(dataList);
        }
        return true;
    }

    /**
     * SELECT t.*
     * FROM order_setting_t t
     * WHERE DATE_FORMAT(order_date,'%Y-%m')='2020-10'
     * @param year
     * @param month
     * @return
     */
    @Override
    public Map<String, Map<String, Integer>> selectDataByMonth(int year, int month) {

        //1.根据年月，先查询数据库
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DATE_FORMAT(order_date,'%Y-%m')",year + "-" + (month < 10 ? "0" : "") + month);
        List<OrderSetting> list = this.list(queryWrapper);//查出整个10月的数据，是个集合
        System.out.println("list = " + list);
        //2.将查询出的数据封装成Map集合
        //返回的数据集合data
        Map<String, Map<String, Integer>> data = new HashMap<>();
        //遍历list集合
        for (OrderSetting row : list) {//集合的每一行元素对应一月的一天
            Map<String, Integer> map = new HashMap<>();
            map.put("number",row.getOrderNumber());
            map.put("reservations",row.getOrdered());
            //data.put('2020-10-19',{number:200,reservations:200})
            data.put(DateUtil.formatDate(row.getOrderDate()),map);
        }
        return data;
    }

    @Override
    public boolean daySetting(String day, int num) {
        OrderSetting orderSetting = findByDate(day);
        if (orderSetting==null){
            orderSetting = new OrderSetting();
        }
        orderSetting.setOrderNumber(num);
        return this.updateById(orderSetting);
    }

    /**
     * 根据日期查询数据库
     * 问题：后台日期类型和数据库日期类型如何匹配查询？？这里可以直接传日期，因为数据库中数据是以字符串格式存储这里也是字符串格式
     * @param orderDate
     * @return
     */
    public OrderSetting findByDate(String orderDate) {
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_date",orderDate);
        return this.getOne(queryWrapper);
    }

    //删除半年前数据
    @Override
    public boolean deleteOrderSettingByDate(String dateString) {

        return this.baseMapper.deleteOrderSettingByDate(dateString);
    }

}
